home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac: Not for Sale / Another.not.for.sale (Australia).iso / fade into you / getting there / TCP⁄PPP⁄SLIP / TCP⁄IP Scripting Addition 1.1.2 / Examples 1.2 / Text Versions / Gopher Server (text) < prev    next >
Text File  |  1994-09-18  |  3KB  |  115 lines

  1. (*
  2. Gopher Server by Atul Butte
  3. version 1.1
  4. Copyright © 1994 by Atul Butte. All Rights Reserved.
  5.  
  6. This is a Gopher daemon. Drag this application to any folder with text files, then launch this application. Those files (and any in any subfolders) will be available via Gopher on port 70.
  7. *)
  8.  
  9. global socketsList
  10. global newsockets
  11. global thePath
  12. global LF
  13. global myIPAddress
  14.  
  15. on run
  16.     set LF to ASCII character (10)
  17.     set myIPAddress to (tcp my address)
  18.     set socketsList to {}
  19.     repeat with x from 1 to 2
  20.         set newSocket to {tcp wait for connect port 70}
  21.         set socketsList to socketsList & newSocket
  22.     end repeat
  23.     
  24.     activate
  25.     set us to path to frontmost application as string
  26.     set oldDelimiters to AppleScript's text item delimiters
  27.     set AppleScript's text item delimiters to {":"}
  28.     set d to text items of us
  29.     set thePath to ((items 1 through -2 of d) as string) & ":"
  30.     set AppleScript's text item delimiters to oldDelimiters
  31. end run
  32.  
  33. on handleFolder(s, l, newPath)
  34.     set allList to list folder newPath
  35.     set folderList to {}
  36.     set textList to {}
  37.     repeat with x in allList
  38.         set i to info for file (newPath & x)
  39.         if (folder of i = true) then
  40.             set folderList to folderList & x
  41.         else if (file type of i = "TEXT") then
  42.             set textList to textList & x
  43.         end if
  44.     end repeat
  45.     repeat with x in folderList
  46.         set sendLine to "1" & x & "    " & l & x & ":" & "    " & myIPAddress & "    70" & return
  47.         tcp write stream s data sendLine using ISO88591
  48.     end repeat
  49.     repeat with x in textList
  50.         set sendLine to "0" & x & "    " & l & x & "    " & myIPAddress & "    70" & return
  51.         tcp write stream s data sendLine using ISO88591
  52.     end repeat
  53. end handleFolder
  54.  
  55. on handleFile(s, l, newPath)
  56.     tcp send stream s source file (newPath) using ISO88591
  57. end handleFile
  58.  
  59. on handleConnection(s)
  60.     try
  61.         repeat until tcp ahead stream s characters LF
  62.         end repeat
  63.         set l to (tcp read stream s until characters LF strip characters return & LF using ISO88591)
  64.         if (offset of "::" in l) > 0 then
  65.             set l to ""
  66.         end if
  67.         set newPath to thePath & l
  68.         try
  69.             set i to info for file (newPath)
  70.             if (folder of i = true) then
  71.                 handleFolder(s, l, newPath)
  72.             else
  73.                 handleFile(s, l, newPath)
  74.             end if
  75.         on error
  76.         end try
  77.         tcp close stream s
  78.     on error msg number num from obj partial result pr
  79.         tcp close stream s
  80.         display dialog "Error: " & msg & " (" & num & ")"
  81.         error {msg, num, obj, pr}
  82.     end try
  83. end handleConnection
  84.  
  85. on idle
  86.     set newsockets to {}
  87.     repeat with x from 1 to (length of socketsList)
  88.         set s to item x of socketsList
  89.         try
  90.             set itsStatus to (tcp status stream s)
  91.             if connection status of itsStatus = Connected then
  92.                 -- if the socket is connected, start a new one waiting and handle the connection
  93.                 set newsockets to newsockets & {tcp wait for connect port 70}
  94.                 handleConnection(s)
  95.             else
  96.                 set newsockets to newsockets & {s}
  97.             end if
  98.         on error
  99.         end try
  100.     end repeat
  101.     set socketsList to newsockets
  102.     set newsockets to {}
  103.     return 10
  104. end idle
  105.  
  106. on quit
  107.     repeat with x from 1 to (length of socketsList)
  108.         set s to item x of socketsList
  109.         try
  110.             tcp close stream s
  111.         on error
  112.         end try
  113.     end repeat
  114.     continue quit
  115. end quit